#ifndef __H_GAME_H__
#define __H_GAME_H__

#include <deque>
#include <vector>
#include <map>
#include <set>
#include "ICC_Timer_Handler.h"
#include "RedisClient.h"
#include "GameBase.h"
#include "DbPart.h"
#include "CClient.h"
#include "HostBase.h"
#include "def.h"
#include "UserinfoExpireMgr.h"

class CHost;

const unsigned int MOD_DENOMINATOR = 20;
const unsigned int g_cache_cool_down_dur = 172800;

class CGameServer : public BYTimerOject
{
public:
	CGameServer();
	virtual ~CGameServer();
public:

	virtual bool InitGame();
	virtual int OnBYTimeOut(int Timerid);
	int ProcessPacket(SocketHandler * pHandler, NETInputPacket* inputPkg);
	int ProcEventGameInit(int ev_id, SpiderEvent* pEvent);
	int ProcEventPublishService(int ev_id, SpiderEvent* pEvent);
private:
	int ProcGetUserinfo(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcUpdateUserinfo(SocketHandler* pHandler, NETInputPacket* inputPkg);
	int ProcGetUserinfoByField(SocketHandler* pHandler, NETInputPacket* inputPkg);
	bool updateAllCache(const unsigned int uid, const unsigned int game_id, stUserinfo* pUserInfo);
	bool updateLocalCacheAll(const unsigned int uid, const unsigned int game_id, stUserinfo* pUserInfo);
	bool updateCacheAll(const unsigned int uid, const unsigned int game_id, const stUserinfo* pUserInfo);
	bool updateToDb(const unsigned int uid);
	int procLocalCacheCheck(void);
	stUserinfo* getUserinfoPtr(const unsigned int uid, const unsigned int game_id);
	stUserinfo* getUserinfoAllFromLocalCache(const unsigned int uid, const unsigned int game_id);
	stUserinfo* getUserinfoAllFromDB(const unsigned int uid, const unsigned int game_id);
	stUserinfo* getUserinfoAllFromCache(const unsigned int uid, const unsigned int game_id);
	void setUserinfoFromRedis(game_userinfo::userinfoDataDb *data, stUserinfo* pUserInfo);
	void setUserinfoToRedis(game_userinfo::userinfoDataDb *data, const stUserinfo* pUserInfo);

	std::uint64_t getKey(const unsigned int uid, const unsigned int game_id);

	UserExpireMgr m_cache{7200}; //2h

	bool m_timerflag;
	unsigned int m_procIndex;
	unsigned int m_procCount;
public:
	CHost* m_pHost;
	RedisClient* m_moneyRedis;
	CDbPart m_dbHandler;
};


#endif


